import com.sun.source.tree.Tree;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 98152
 * Date: 2024-07-22
 * Time: 14:45
 */
public class BinarySearchTree {

    static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    public TreeNode root ;

    public void insert(int key) {
        if(root == null) {
            root = new TreeNode(key);
            return;
        }
        TreeNode parent = null;
        TreeNode cur = root;
        TreeNode node = new TreeNode(key);
        while(cur != null) {
            parent = cur;
            if(cur .val < key) {
                cur = cur.right;
            }else if(cur.val > key) {
                cur = cur.left;
            }else {
                return;
            }
        }
        if(parent.val < key) {
            parent.right = node;
        }else {
            parent.left = node;
        }
    }

    public TreeNode search(int val) {
        TreeNode cur = root;
        while(cur != null && cur.val != val) {
            if(cur.val < val) {
                cur = cur.right;
            }else {
                cur = cur.left;
            }
        }
        return cur;
    }

    /*else {
        TreeNode target = cur.right;
        TreeNode targetParent = cur;
        while(target.left != null) {
            targetParent = target;
            target = target.left;
        }
        cur.val = target.val;
        if(targetParent.left == target) {
            targetParent.left = target.right;
        }else {
            targetParent.right = target.right;
        }
    }*/
}